﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace UCHENG.FILEHASH.UTILS
{
    public class ZipService
    {
        public static void Excute(string downloadPlayerPath, string PPTPlayerPath, bool isReplace)
        {
            copyZipTo101PPT(downloadPlayerPath, PPTPlayerPath);

            File.Delete(downloadPlayerPath);

            string targetPath = PPTPlayerPath + "\\" + Path.GetFileName(downloadPlayerPath);
            unZip(targetPath, PPTPlayerPath + "\\" + Path.GetFileNameWithoutExtension(downloadPlayerPath), isReplace);          
        }

        private static void copyZipTo101PPT(string downloadPlayerPath, string PPTPlayerPath)
        {
            try
            {
                string targetPath = PPTPlayerPath + "\\" + Path.GetFileName(downloadPlayerPath);
                File.Copy(downloadPlayerPath, targetPath, true);
            }
            catch (Exception ex)
            {
            }
        }


        public static bool renameDir(DirectoryInfo dir, string newPath)
        {
            try
            {
                dir.MoveTo(newPath);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }


        #region 解压  

        /// <summary>   
        /// 解压功能(解压压缩文件到指定目录)   
        /// </summary>   
        /// <param name="fileToUnZip">待解压的文件</param>   
        /// <param name="zipedFolder">指定解压目标目录</param>   
        /// <returns>解压结果</returns>   
        private static bool unZip(string fileToUnZip, string zipedFolder, bool isReplace)
        {
            bool result = true;
            if (!File.Exists(fileToUnZip))
                return false;

            if (!Directory.Exists(zipedFolder))
                Directory.CreateDirectory(zipedFolder);



            if (Directory.Exists(zipedFolder))
            {
                if (!isReplace)
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(zipedFolder);
                    renameDir(directoryInfo, zipedFolder + "_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss"));
                }
                else
                {
                    //Directory.Delete(zipedFolder);
                    FileService.DeleteDir(new DirectoryInfo(zipedFolder));
                }
            }

            try
            {
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
                MessageService.start_unzip(Path.GetFileName(fileToUnZip));

                ZipHelper zipHelper = new ZipHelper();
                zipHelper.UnZip(fileToUnZip, zipedFolder);

                MessageService.end_unzip(Path.GetFileName(fileToUnZip), stopwatch.Elapsed.TotalSeconds);
                stopwatch.Stop();
            }
            catch
            {
                result = false;
            }
            finally
            {
                GC.Collect();
                GC.Collect(1);
            }
            return result;
        }


        #endregion

    }
}
